from django.core.paginator import Paginator
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from data.cartItems import context as cartContext
# from data.itemList import context as itemContext
from items.models import ItemList


def list(request):
    page = request.GET.get('page')
    if not page:
        page = 1
    # item = Paginator(cartContext, per_page=4).page(int(page))
    return render(request, 'cart/list.html', {'list': cartContext.values})


def add(request, item_idx):
    if request.method == 'POST':
        if item_idx in cartContext:
            cartItem = cartContext[item_idx]
            cartItem['count'] += 1
        else:
            item = ItemList.objects.get(id=item_idx)
            cartItem = {'id': item.id, 'price': item.price, 'title': item.title, 'postal': item.postal,
                        'shopNick': item.shopNick, 'payNum': item.payNum, 'count': 1, 'image': item.image}
            cartContext[item_idx] = cartItem

        return HttpResponseRedirect(reverse('items:index'))
        # return render(request, 'items/list.html', {'list': itemContext})
    else:
        return HttpResponse('failed')


def delete(request):
    return HttpResponse('haha')
    # del context['list'][]
    # return render(request, 'cart/list.html', context)
